﻿using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Json;
using Newtonsoft.Json;

namespace Helper
{
    public class Json<T>
    {
        /// <summary>
        /// Преобразовать любой тип в JSON
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string NewtonSerializeObject(object obj)
        {
            return  JsonConvert.SerializeObject(obj);                 
        }
        
        /// <summary>
        /// Преобразовать JSON в заданный тип
        /// </summary>
        /// <param name="inType">заданный тип</param>
        /// <param name="json">json</param>
        public static T NewtonDeserializeObject(string json)
        {
            return JsonConvert.DeserializeObject<T>(json);
            
        }

        /// <summary>
        /// Преобразовать  объект в JSON строку  
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string Serialize(T obj)
        {
            System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            MemoryStream ms = new MemoryStream();
            serializer.WriteObject(ms, obj);
            string retVal = Encoding.Default.GetString(ms.ToArray());
            ms.Dispose();
            string s= Encoding.UTF8.GetString(Encoding.Default.GetBytes(retVal) );
            return s;
        }

        /// <summary>
        /// Преобразовать json в заданный тип
        /// </summary>
        /// <param name="inType">заданный тип</param>
        /// <param name="json">json</param>
        public static T Deserialize(string json)
        {
            json=ClearJson (json);
            
            MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
            try
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                T result = (T)serializer.ReadObject(ms);
                return result;
            }
            
            finally
            {
                ms.Close();
            }
        }

        public static string ClearJson(string json)
        {
            return json.Replace(@"\", string.Empty).Replace("Ð",string.Empty  );
            
        }

        /// <summary>
        /// Преобразовать List в JSON массив
        /// </summary>
        /// <param name="lst"></param>
        /// <returns></returns>
        public static string ListToJson(List<T> lst)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            for (int i=0;i<lst.Count ;i++)
            {
                string s = Helper.Json<T>.Serialize(lst[i]);                
                sb.Append(s);
                sb.Append(",");
            }
            if(lst.Count >0)
                sb.Remove(sb.Length - 1, 1);

            sb.Append("]");
            return sb.ToString();
        }
    }
}
